<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <mall@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\mall\controller\admin;

use app\mall\base\MallAdminBase;

use app\mall\model\MallSpecCats as MallSpecCatsModel;
use app\mall\model\MallSpecGroups as MallSpecGroupsModel;
use app\mall\model\MallSpecItems as MallSpecItemsModel;

class SpecGroups extends MallAdminBase{
    //当前控制器菜单
    public $submenu = [];

    /**
     * TP初始化方法
     */
    public function _initialize() {
        parent::_initialize();
        $this->submenu[] = ['title' => '规格分类列表', 'url' => url('mall/admin.specCats/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '规格组列表', 'url' => url('mall/admin.specGroups/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '规格列表', 'url' => url('mall/admin.specItems/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '添加规格组', 'url' => url('mall/admin.specGroups/add'), 'icon' => '&#xe608;'];
        $this->submenu[] = ['title' => '更新规格缓存', 'url' => url('mall/admin.specCats/cache'), 'icon' => 'ဂ'];
    }

    /**
     * 后台商品规格组列表
     * @access public
     * @return \think\Response
     */
    public function index($catid = 0){
        if($catid){
            $list = MallSpecGroupsModel::where('spec_catid',$catid)->order('id desc')->paginate(config('mn_admin.admin_pagesize'));
        }else{
            $list = MallSpecGroupsModel::order('id desc')->paginate(config('mn_admin.admin_pagesize'));
        }
        $this->assign([
            'submenu' => $this->submenu,
            'list' => $list
        ]);
        return $this->fetch();
    }

    /**
     * 后台商品规格组添加
     * @access public
     * @return \think\Response
     */
    public function add(){
        // POST提交数据
        if($this->request->isPost()){
            $post_data = $this->buildParam();
            // 实例化商品规格组模型
            $MallSpecGroupsModel = new MallSpecGroupsModel;
            // 验证商品规格组在当前规格分类是否重复
            $check_repeat = MallSpecGroupsModel::where('spec_catid',$post_data['spec_catid'])
                                                ->where('name',$post_data['name'])
                                                ->count();
            if($check_repeat){
                $this->error('商品规格组名称重复');
            }
            if($MallSpecGroupsModel->allowField(true)->validate(true)->save($post_data)){
                // 更新所有规格缓存
                $MallSpecCatsModel = new MallSpecCatsModel;
                $MallSpecCatsModel->cache();
                $this->success('商品规格组添加成功','index');
            }
            else{
                $this->error($MallSpecGroupsModel->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu,
            'spec_cats' => MallSpecCatsModel::all()
        ]);
        return $this->fetch();
    }

    /**
     * 后台商品规格组修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少商品规格组id');
        }
        // 实例化商品规格组模型
        $spec_cat = MallSpecGroupsModel::get($id);
        if(!$spec_cat){
            $this->error('商品规格组不存在','index');
        }
        // POST提交数据
        if($this->request->isPost()){
            $post_data = $this->buildParam();
            // 验证商品规格组在当前规格分类中是否重复
            $check_repeat = MallSpecGroupsModel::where('id','neq',$id)
                                                ->where('spec_catid',$post_data['spec_catid'])
                                                ->where('name',$post_data['name'])
                                                ->count();
            if($check_repeat){
                $this->error('商品规格组名称重复');
            }
            if(false !== $spec_cat->allowField(true)->validate(true)->save($post_data,['id' => $id])){
                // 更新所有规格缓存
                $MallSpecCatsModel = new MallSpecCatsModel;
                $MallSpecCatsModel->cache();
                $this->success('商品规格组修改成功','index');
            }
            else{
                $this->error($spec_cat->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu,
            'data'      => $spec_cat->getData(),
            'spec_cats' => MallSpecCatsModel::all()
        ]);
        return $this->fetch();
    }

    /**
     * 后台商品规格组删除
     * @access public
     * @return void
     */
    public function del($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少商品规格组id');
        }
        $spec_group = MallSpecGroupsModel::get($id);
        if(!$spec_group){
            $this->error('需要删除的商品规格组不存在');
        }
        // 删除规格组下的所有规格值
        MallSpecItemsModel::destroy(['spec_group_id' => $id]);
        // 删除规格组
        $spec_group->delete();
        // 更新所有规格缓存
        $MallSpecCatsModel = new MallSpecCatsModel;
        $MallSpecCatsModel->cache();
        $this->success('商品规格组删除成功', 'index');
    }
}